#encoding: UTF-8
from exts import db
from datetime import datetime
from werkzeug.security import generate_password_hash,check_password_hash

class User(db.Model):
	__tablename__ = 'user'
	id = db.Column(db.Integer, primary_key=True, autoincrement=True)
	username = db.Column(db.String(100), nullable=False)
	password = db.Column(db.String(100), nullable=False)

	def __init__(self,*args,**kwargs):
		username = kwargs.get('username')
		password = kwargs.get('password')
		self.username = username
		self.password = generate_password_hash(password)

	def check_password(self, raw_password):
		result = check_password_hash(self.password, raw_password)
		return result


class Post(db.Model):
	__tablename__ = 'post'
	id = db.Column(db.Integer, primary_key = True, autoincrement = True)
	title = db.Column(db.String(100), nullable = False)
	content = db.Column(db.Text, nullable = False)
	time = db.Column(db.DateTime, default = datetime.now)
	author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
	author = db.relationship('User', backref = db.backref('posts'))


class Comment(db.Model):
	__tablename__ = 'comment'
	id = db.Column(db.Integer, primary_key = True, autoincrement = True)
	content = db.Column(db.Text, nullable = False)
	time = db.Column(db.DateTime, default = datetime.now)
	author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
	author = db.relationship('User', backref = db.backref('comments'))
	post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
	post = db.relationship('Post', backref = db.backref('comments', order_by = time.desc()))